﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Web.UI;
using MxWeiXinPF.Common;
using System.Web.UI.HtmlControls;
using System.Data;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class userratepoints_edit : ManagePage
    {
        private string action = MXEnums.ActionEnum.Add.ToString();
        protected MxWeiXinPF.Model.manager admin_info = null;
        private int id = 0;
        private int wid = 0;
        private string openid = "";
        private decimal historyRate = 0m;
        protected void btnAudit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtconfirmcode.Text.Trim()))
            {
                base.JscriptMsg("兑换码不能为空！", "", "Error");
            }
            else if (string.IsNullOrEmpty(this.txtAmount.Text.Trim()))
            {
                base.JscriptMsg("兑换的积分数不能为空！", "", "Error");
            }
            else if (!this.DoAudit(this.id))
            {
                base.JscriptMsg("审核过程中发生错误！", "", "Error");
            }
            else
            {
                base.JscriptMsg("审核成功，积分兑换操作完成！", "", "Success");
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtconfirmcode.Text.Trim()))
            {
                base.JscriptMsg("兑换码不能为空！", "", "Error");
            }
            else if (string.IsNullOrEmpty(this.txtAmount.Text.Trim()))
            {
                base.JscriptMsg("兑换的积分数不能为空！", "", "Error");
            }
            else if (!this.DoUpdate())
            {
                base.JscriptMsg("保存过程中发生错误！", "", "Error");
            }
            else
            {
                base.JscriptMsg("保存成功！", "", "Success");
            }
        }

        private bool DoAudit(int _id)
        {
            GetJifen();
            bool flag = false;
            BLL.user_rate _rate = new BLL.user_rate();
            int id = int.Parse(this.hid.Value.Trim());

            decimal amount = decimal.Parse(this.txtAmount.Text.ToString().Trim());
            if (amount <= 0m)
            {
                base.JscriptMsg("兑换的积分数不能为0或者负数！", "", "Error");
                return false;
            }
            if (amount > historyRate)
            {
                base.JscriptMsg("积分数量少于用户当前积分！", "", "Error");
                return false;
            }

            if (_rate.AuditPoints("1", this.txtconfirmcode.Text.Trim(), id))
            {
                base.AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "审核积分兑换:" + id);
                flag = true;
            }
            return flag;
        }

        private bool DoUpdate()
        {
            bool flag = false;
            BLL.user_rate _rate = new BLL.user_rate();
            GetJifen();
            decimal amount = decimal.Parse(this.txtAmount.Text.ToString().Trim());
            if (amount <= 0m)
            {
                base.JscriptMsg("兑换的积分数不能为0或者负数！", "", "Error");
                return false;
            }
            //if (amount > historyRate)
            //{
            //    base.JscriptMsg("积分数量少于用户当前积分！", "", "Error");
            //    return false;
            //}
            int id = int.Parse(this.hid.Value.Trim());
            if (_rate.UpdatePoints(this.admin_info.id, -amount, this.txtremarks.Text.Trim(), this.txtconfirmcode.Text.Trim(), id))
            {
                base.AddAdminLog(MXEnums.ActionEnum.Add.ToString(), string.Concat(new object[] { "更新积分兑换:", id, "-", this.txtAmount.Text }));
                flag = true;
            }
            return flag;
        }

        private void GetInfoCode(string confirmcode)
        {
            DataSet set = new BLL.user_rate().AdminGetListByConfirmCode(confirmcode);
            if ((set != null) && (set.Tables[0].Rows.Count > 0))
            {
                for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                {
                    this.id = int.Parse(set.Tables[0].Rows[0]["id"].ToString());
                    this.hid.Value = set.Tables[0].Rows[0]["id"].ToString();
                    this.txtconfirmcode.Text = set.Tables[0].Rows[0]["confirmcode"].ToString();
                    this.txtAmount.Text = set.Tables[0].Rows[0]["amount"].ToString();
                    this.txtremarks.Text = set.Tables[0].Rows[0]["remarks"].ToString();
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string queryString = MXRequest.GetQueryString("action");
                MxWeiXinPF.Model.wx_userweixin weiXinCode = base.GetWeiXinCode();
                this.admin_info = base.GetAdminInfo();
                this.wid = weiXinCode.id;
                if (!(string.IsNullOrEmpty(queryString) || !(queryString == MXEnums.ActionEnum.Edit.ToString())))
                {
                    this.action = MXEnums.ActionEnum.Edit.ToString();
                    this.id = MXRequest.GetQueryInt("id");
                    this.openid = MXRequest.GetStringValue("openid", "");
                }

                if (!this.Page.IsPostBack)
                {
                    base.ChkAdminLevel("userratepoints_list", MXEnums.ActionEnum.View.ToString());
                    if ((this.action == MXEnums.ActionEnum.Edit.ToString()) && (this.id > 0))
                    {
                        this.ShowInfo(this.id);
                    }
                }
            }
            catch (Exception)
            {
                
            }

        }

        private void ShowInfo(int _id)
        {
            DataSet set = new BLL.user_rate().AdminGetListByID(_id);
            if ((set != null) && (set.Tables[0].Rows.Count > 0))
            {
                for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                {
                    this.id = int.Parse(set.Tables[0].Rows[0]["id"].ToString());
                    this.hid.Value = set.Tables[0].Rows[0]["id"].ToString();
                    this.txtconfirmcode.Text = set.Tables[0].Rows[0]["confirmcode"].ToString();
                    if (set.Tables[0].Rows[0]["amount"] == null || set.Tables[0].Rows[0]["amount"].ToString() == "")
                    {
                        this.txtAmount.Text = "0.00";
                    }
                    else
                    {
                        decimal amount = Convert.ToDecimal(set.Tables[0].Rows[0]["amount"].ToString());
                        this.txtAmount.Text = (-amount).ToString();
                    }

                    this.txtremarks.Text = set.Tables[0].Rows[0]["remarks"].ToString();
                }
            }
        }

        protected void txtconfirmcode_TextChanged(object sender, EventArgs e)
        {
            if (!this.txtconfirmcode.Text.Trim().Equals(""))
            {
                this.GetInfoCode(this.txtconfirmcode.Text.Trim());
            }
        }

        private void GetJifen()
        {
            if (string.IsNullOrEmpty(this.openid))
            {
                return;
            }
            DataSet dateSetByWidOpenID = new MxWeiXinPF.BLL.user_rate().GetDateSetByWidOpenID(openid);
            if ((dateSetByWidOpenID != null) && (dateSetByWidOpenID.Tables[0].Rows.Count > 0))
            {
                if ((dateSetByWidOpenID.Tables[0].Rows[0]["hisRate"].ToString() != null) && (dateSetByWidOpenID.Tables[0].Rows[0]["hisRate"].ToString() != ""))
                {
                    this.historyRate = Convert.ToDecimal(dateSetByWidOpenID.Tables[0].Rows[0]["hisRate"].ToString());
                }
            }
        }
    }
}